﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace YouHP.BLL.MedicalGraph
{
    public class GraphTreatment : GraphBase
    {
        #region data and param
        class Variable
        {
            public string mName = "";
            public string mKey = "";
        }

        class GroupVariable
        {
            internal string mName = "";
            internal List<Variable> mVariables = new List<Variable>();

            internal void AddVariable(Variable var)
            {
                mVariables.Add(var);
            }
        }
        /*
        List<GroupVariable> mGroups = new List<GroupVariable>();
        public int AddGroup(string groupname)
        {
            GroupVariable group = new GroupVariable();
            group.mName = groupname;
            mGroups.Add(group);
            return mGroups.Count - 1;
        }
        public void AddVariable(int idx, string name, string key)
        {
            Variable var = new Variable();
            var.mName = name;
            var.mKey = key;
            mGroups[idx].AddVariable(var);
        }
        */

        class ValueOption
        {
            public string mName;
            public string mValue;
            private Color mColor;
            public Brush mBrush;
            public Pen mPen;

            public void SetColor(Color color)
            {
                mColor = color;
                mBrush = new SolidBrush(color);
                mPen = new Pen(color, 2.0f);
            }
        }
        List<ValueOption> mValueOption = new List<ValueOption>();

       
        public void AddOption(string name, string value, Color color)
        {
            ValueOption opt = new ValueOption();
            opt.mName = name;
            opt.mValue = value;
            opt.SetColor(color);
            mValueOption.Add(opt);
        }
        ValueOption GetOption(string value)
        {
            foreach (ValueOption opt in mValueOption)
            {
                if (value == opt.mValue) return opt;
            }
            return null;
        }



        #endregion

        public GraphTreatment()
        {
            mLegendName = "治疗目的";

            this.AddOption("治疗本病", "0", System.Drawing.Color.Red);
            this.AddOption("治疗本病或其他疾病", "1", System.Drawing.Color.FromArgb(255, 153, 0));
            this.AddOption("保健", "2", System.Drawing.Color.FromArgb(51, 153, 102));
            this.AddOption("其他或不清楚", "3", System.Drawing.Color.White);
        }

     
      

        #region SetupGrid
        Dictionary<int, int> mYAxis = new Dictionary<int, int>();
        protected override void SetupAxis()
        {
            //int cnt1 = mGroups.Count;
            //int y = _DataRect.Top;
            //for (int i = 0; i < cnt1; i++)
            //{
            //    mYAxis.Add(i * 100 + 99, y);
            //    y += 30;
            //    int cnt2 = mGroups[i].mVariables.Count;
            //    for (int j = 0; j < cnt2; j++)
            //    {
            //        mYAxis.Add(i * 100 + j, y);
            //        y += 30;
            //    }
            //}

            int cnt1 = Rule.mGroups.Count;
            int y = _DataRect.Top;
            for (int i = 0; i < cnt1; i++)
            {
                mYAxis.Add(i * 100 + 99, y);
                y += 30;
                int cnt2 = Rule.mGroups[i].mVariables.Count;
                int j = 0;
                for (j = 0; j < cnt2; j++)
                {
                    mYAxis.Add(i * 100 + j, y);
                    y += 30;
                }
                if (i == 0 && j < 5) y += 30 * (5 - j);
            }            
        }

        protected override double GetDataY(int index, double y)
        {
            return mYAxis[index];
        }
        #endregion

        #region Draw

        protected override void DrawData()
        {
            
            int j, k;
            int cnt2, cnt3;
            int x1, x2;
            int y1;
            string str1;
            Pen pen = new Pen(Color.Black, 1.0f);



            Rectangle rect;

            //MedicalDataInfo data = Data[i - 1];
            //x1 = (int)this.GetDataX(0, i);
            //y1 = _DataRect.Bottom + 10;
            //DrawDataTime(data.CheckDate, x1, y1);
            cnt2 = Rule.GetGroupCount();
            for (j = 0; j < cnt2; j++)
            {
                GraphRule.Group group = Rule.mGroups[j];
                cnt3 = group.mVariables.Count;

                for (k = 0; k < cnt3; k++)
                {
                    int indexkey = j * 100 + k;
                    y1 = (int)this.GetDataY(j * 100 + k, 0);
                    GraphRule.Variable var = group.mVariables[k];
                    GraphDataVariableInfo dataVar = Data.GetDataVariable(var.mKey);
                    if (dataVar == null) return;
                    if (dataVar.DataItemList == null) continue;
                    foreach (GraphDataItemInfo dataItem in dataVar.DataItemList)
                    {
                        str1 = dataItem.Value;
                        x2 = (int)GetDataX(0, dataItem.DateTime1);
                        x1 = (int)GetDataX(0, dataItem.DateTime2);
                        ValueOption opt = this.GetOption(str1);
                        if (opt != null)
                        {
                            rect = new Rectangle(x2, y1 - 8, x1 - x2 - 2, 16);
                            _GraphicsData.FillRectangle(opt.mBrush, rect);
                            _GraphicsData.DrawRectangle(opt.mPen, rect);
                            rect = new Rectangle(x1, y1 - 8, 3, 16);
                            _GraphicsData.FillRectangle(opt.mBrush, rect);
                            _GraphicsData.DrawRectangle(opt.mPen, rect);
                        }
                    }
                }
            }
            
        }

        protected override void DrawDataGrid()
        {
            int x1, x2;
            int y1, y2;
            int i;
            int count;

            y1 = this._DataRect.Top;
            y2 = this._DataRect.Bottom;
            for (i = 1; i <= _GridCount; i++)
            {
                x1 = (int)this.GetDataX(0, i);
                _GraphicsData.DrawLine(Rule.mGridPen, x1, y1, x1, y2);
                _GraphicsData.DrawString(i.ToString(),
                    Rule.mGridFont, Brushes.Black, x1, y2, Rule.mLabelFotmat);
            }

            for (i = 0; i < MonthCount; i++)
            {
                DateTime dt = _StartDateTime.AddMonths(i);
                x1 = (int)this.GetDataX(0, i+1);
                y1 = _DataRect.Bottom + 10;
                DrawDataTime(dt, x1, y1);
            }

            Rectangle rect = new Rectangle(0, 0, Rule.DataWidth, Rule.Height);
            x1 = rect.Left;
            x2 = rect.Right - 10;
            y1 = rect.Bottom - 30;
            _GraphicsData.DrawLine(Rule.mXAxisPen, x1, y1, x2, y1);

            count = Rule.GetGroupCount();
            for (i = 0; i < count; i++)
            {
                x1 = rect.Left;
                x2 = rect.Right - 10;
                y1 = (int)this.GetDataY(i * 100 + 99, 0);
                _GraphicsData.DrawLine(Rule.mGridPen, x1, y1, x2, y1);
            }

        }

        protected override void DrawLegendGrid()
        {
            int i, j;
            int x1, x2, x3;
            int y1, y2;
            Pen pen1;
            Font font1, font2;
            int cnt1, cnt2;

            cnt1 = Rule.GetVariableCount();
            x1 = 10;
            x2 = Rule.LegendWidth - 5;
            x3 = Rule.LegendWidth;

            StringFormat dformat1 = new StringFormat();
            dformat1.Alignment = StringAlignment.Near;
            dformat1.LineAlignment = StringAlignment.Center;
            StringFormat dformat2 = new StringFormat();
            dformat2.Alignment = StringAlignment.Far;
            dformat2.LineAlignment = StringAlignment.Center;
            font1 = new Font("宋体", 12.0f, FontStyle.Bold);
            pen1 = new Pen(Color.Black, 2.0f);
            font2 = new Font("宋体", 9.0f);
            cnt1 = Rule.mGroups.Count;
            for (i = 0; i < cnt1; i++)
            {
                GraphRule.Group group = Rule.mGroups[i];
                y1 = (int)this.GetDataY(i * 100 + 99, 0);
                mGraphicsLegend.DrawLine(Style.GridPen, x1, y1, x3, y1);
                mGraphicsLegend.DrawString(group.mName, font1, Brushes.Black, x1 + 10, y1 + 15, dformat1);
                cnt2 = group.mVariables.Count;
                for (j = 0; j < cnt2; j++)
                {
                    GraphRule.Variable var = group.mVariables[j];
                    y1 = (int)this.GetDataY(i * 100 + j, 0);
                    mGraphicsLegend.DrawLine(pen1, x2, y1, x2 + 5, y1);
                    mGraphicsLegend.DrawString(var.mName, font2, Brushes.Black, x2 - 6, y1, dformat2);
                }
            }
            y1 = Rule.Height - 30;
            mGraphicsLegend.DrawLine(pen1, x2, y1, _DataRect.Right, y1);
            y2 = (int)this.GetDataY(99, 0);
            mGraphicsLegend.DrawLine(pen1, x2, y1, x2, y2);
        }

        protected override void DrawLegend()
        {
            int i;
            int x1, x2;
            int y1, y2;
            Pen pen1;
            Font font1;
            Rectangle rect1;
            int cnt1;
            StringFormat dformat;

            Graphics g = mGraphicsLegend;
            dformat = new StringFormat();


            dformat.Alignment = StringAlignment.Near;
            dformat.LineAlignment = StringAlignment.Center;
            font1 = new Font("宋体", 9.0f);
            x1 = 40;
            x2 = x1 + 60;
            cnt1 = mValueOption.Count;
            pen1 = new Pen(Color.Black, 1.0f);
            y1 = 70;
            y1 += 10;
            g.DrawString(mLegendName, font1, Brushes.Black, x1, y1);
            y1 += 20;
            int width = 0;
            for (i = 0; i < cnt1; i++)
            {
                ValueOption option = mValueOption[i];
                y2 = y1 + 10;
                rect1 = new Rectangle(x1, y1, 50, 15);
                g.FillRectangle(option.mBrush, rect1);
                g.DrawRectangle(pen1, rect1);
                g.DrawString(option.mName, font1, Brushes.Black, x2, y2, dformat);
                y1 += 20; ;
                int w = (int)(g.MeasureString(option.mName, font1).Width);
                if (width < w) width = w;
            }
            g.DrawRectangle(pen1, x1 - 10, 70, width + 70, cnt1 * 20 + 35);

        }

        #endregion

        #region Setup Page
        protected override void SetupPage()
        {
            //WJQ _GridCount = Param.mGridCount > Data.Count ? Param.mGridCount : Data.Count;

            DateTime dt = DateTime.Now;
            string timestr = dt.ToString("yyyyMMddhhmmss");
            mDataPath = Path.Combine(SavePath, string.Format("Sym_{1}_Data_{0}.png", Suffix, timestr));
            mLegendPath = Path.Combine(SavePath, string.Format("Sym_{1}_Legend_{0}.png", Suffix, timestr));
            mWebDataPath = Path.Combine(WebPath, string.Format("Sym_{1}_Data_{0}.png", Suffix, timestr));
            mWebLegendPath = Path.Combine(WebPath, string.Format("Sym_{1}_Legend_{0}.png", Suffix, timestr));
            Result.GraphWebPath += "|" + mWebLegendPath;
            Result.GraphWebPath += "|" + mWebDataPath;

            // 设置
            dt = Data.MinDateTime;
            _StartDateTime = new DateTime(dt.Year, dt.Month, 1);
            dt = Data.MaxDateTime.AddMonths(1);
            _EndDateTime = new DateTime(dt.Year, dt.Month, 1);
            MonthCount = (Data.MaxDateTime.Year - Data.MinDateTime.Year) * 12;
            MonthCount += (Data.MaxDateTime.Month - Data.MinDateTime.Month) + 2;
            _GridCount = MonthCount;
            if (_GridCount < 15) _GridCount = 15;
            Rule.DataWidth = _GridCount * 30 + 30;


            int height = 30;
            int cnt1 = Rule.mGroups.Count;
            for (int i = 0; i < cnt1; i++)
            {
                GraphRule.Group group = Rule.mGroups[i];
                int cnt2 = group.mVariables.Count;
                for (int j = 0; j < cnt2; j++)
                {
                    height += 30;
                }
                height += 30;
            }
            height += 60;
            Rule.Height = Rule.Height > height ? Rule.Height : height;

            _DataRect = new Rectangle(0, 30, Rule.DataWidth, Rule.Height - 60);
        }
        #endregion
    }
}
